package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private HttpSession session;
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String,String> map){
        //1. 接收参数
        String phone = map.get("phone");

        //2. 生成一个验证码 todo 临时写死验证,等到上线再放开
//        String code = RandomUtil.randomNumbers(6);
        String code = "123";
        log.info("生成的验证码是:{}", code);

        //3. 发送验证码 todo 等到生成环境再放开  为了省点钱
//        smsTemplate.sendSms(phone, code);

        //4. 记录下发送的验证码,方便后期对比
        //session.setAttribute("USER_PHONE_CODE" + phone, code);// phone:15612345678 ,123
        //为了防止多访问问题 将验证码缓存到redis中 设置验证码存在时间五分钟
        redisTemplate.opsForValue().set("USER_PHONE_CODE:" + phone, code,5, TimeUnit.MINUTES);
        return ResultInfo.success(null);
    }
    //前端登录验证
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
        //接收phone code
        String phone = map.get("phone");
        String code = map.get("code");
        //通过session得到验证码 先对比验证码 如果失败返回错误信息
//        String codeFromeSession =(String) session.getAttribute("USER_PHONE_CODE" + phone);
//        if (StringUtils.isEmpty(codeFromeSession)||!StringUtils.equals(code,codeFromeSession)){
//            return ResultInfo.error("验证码不同");
//        }
        //从redis中提取验证码 对比
        String codeFromeRedis =(String) redisTemplate.opsForValue().get("USER_PHONE_CODE:" + phone);
        if (StringUtils.isEmpty(codeFromeRedis)||!StringUtils.equals(code,codeFromeRedis)){
            return ResultInfo.error("验证码不同");
        }
        //调用service查询当前手机号在库中是否存在
        User user=userService.findByPhone(phone);
        if (user!=null){
            //判断用户账号权限
         if (user.getStatus()==0) {
             return ResultInfo.error("该用户为被激活");
         }
        }else {
            //查到 吧用户保存到其中
            User user1 = new User();
            user1.setPhone(phone);
            user1.setStatus(1);
            userService.save(user1);
        }
        //用户信息放入session
        session.setAttribute(Constant.SESSION_USER, user);
        return ResultInfo.success(null);
    }
    //退出登录
    @PostMapping("/user/logout")
    public ResultInfo over(){
        session.invalidate();
        return ResultInfo.success(null);
    }
}
